package com.gimi.cloud.bbp.client.service.product.impl;

import com.gimi.cloud.bbp.client.service.product.SkuService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelValueDao;
import com.gimi.cloud.bbp.dao.dao.prod.ProdSkuImgDao;
import com.gimi.cloud.bbp.dao.dao.product.ProductSkuDao;
import com.gimi.cloud.bbp.dto.prod.EcProdCatModelValueDTO;
import com.gimi.cloud.bbp.dto.product.ProductSkuDto;
import com.gimi.cloud.bbp.entity.product.ProductSkuEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Sku变价,修改库存
 * @author chendi
 * @date 2017年12月26日
 */
@Service
public class SkuServiceImpl implements SkuService {

    private final ProductSkuDao skuDao;
    private final EcProdCatModelValueDao modelValueDao;
    private final ProdSkuImgDao prodSkuImgDao;

    @Autowired
    public SkuServiceImpl(ProductSkuDao skuDao, EcProdCatModelValueDao modelValueDao, ProdSkuImgDao prodSkuImgDao){
        this.skuDao = skuDao;
        this.modelValueDao = modelValueDao;
        this.prodSkuImgDao = prodSkuImgDao;
    }

    @Override
    public List<ProductSkuDto> getSkuList(String productId) {
    	List<ProductSkuDto> list = skuDao.selectByProdId(productId);//(1,7)
    	
    	//关联查询,SkuValue对应的model及model_value值(如,颜色:红色)
    	for (ProductSkuDto productSkuDto : list) {
    		List<Map<String, Object>> map = modelValueDao.getModelIdByIds(productSkuDto.getSkuValue());
    		productSkuDto.setSkuValueDetail(map);
		}
    	
        return list;
    }

	@Override
	public void saveSkuList(List<ProductSkuDto> list) {
		for (ProductSkuDto productSkuDto : list) {
			ProductSkuEntity record = new ProductSkuEntity();
			BeanUtils.copyProperties(productSkuDto, record);
			//只修改价格库存,不修改skuValue值
			record.setSkuValue(null);
			skuDao.updateByPrimaryKeySelective(record);
		}
	}

	@Override
	public String getProdStatus(String prodId) {
		int status = skuDao.getStatusById(prodId);
		
		return status + "";
	}



    @Override
    public List<Map<String,Object>> getSkuModelDetail(String skuValue) {
        List<Map<String,Object>> list = new ArrayList<>();
        if(!StringUtil.isBlank(skuValue)){
            String[] values = skuValue.split(",");
            if(values.length > 0){
                for (int i =0;i<values.length;i++){
                    Map<String,Object> map = new HashMap<>();
                    EcProdCatModelValueDTO dto = modelValueDao.getModelIdById(Integer.parseInt(values[i]));
                    map.put("modelId",dto.getModelId());
                    map.put("valueId",dto.getCmId());
                    map.put("valueName",dto.getModelValue());
                    map.put("isImg",dto.getIsImg());
                    list.add(map);
                }
            }
        }
        return list;
    }

	@Override
	public void deleteOne(ProductSkuDto dto) {
		int i = skuDao.deleteByPrimaryKey(dto.getSkuId());
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.FAILURE, "操作失败,sku_id错误!");
		}
	}
	
	@Override
	public List<String> findSkuImg(ProductSkuDto dto) {
		List<String> list = prodSkuImgDao.findSkuImg(dto.getSkuId());
		return list;
	}

}
